package com.bmldyq.family.front.sysManager.service.impl;

import com.bmldyq.family.common.vo.AvailableResult;
import com.bmldyq.family.front.sysManager.fallback.HystrixFallback;
import com.bmldyq.family.front.sysManager.proivder.CoderProivder;
import com.bmldyq.family.front.sysManager.service.CodeService;
import com.bmldyq.family.model.SysParam;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 熔断有两种方式
 * hstrix+ribbo的方式 HystrixCommand配置回调方法
 * hstrix+feign的方式
 */
@Service
@Slf4j
public class CodeServiceImpl implements CodeService {

    @Autowired
    private CoderProivder coderProivder;

    @Autowired
    private RestTemplate restTemplate;
    @Override
    public AvailableResult<SysParam> getAllParam() {
        return coderProivder.getAllParam();
    }

    @Override
    @HystrixCommand(fallbackMethod = "getHystrixErr")
    public AvailableResult<SysParam> getParam() {
        ResponseEntity<AvailableResult> response =   restTemplate.getForEntity("http://base-info/base/codes/get", AvailableResult.class);
       HttpStatus status =  response.getStatusCode();
       AvailableResult<SysParam> ava = response.getBody();
        HttpHeaders headers = response.getHeaders();
        log.info("++++++++++++status={}",status);
        log.info("++++++++++++ava={}",ava);
        log.info("++++++++++++headers={}",headers.toString());
        return ava;
    }


    public AvailableResult<SysParam> get2() {
        AvailableResult<SysParam> ava = coderProivder.get();
        log.info("++++++++++++ava={}",ava);
        return ava;
    }

    public AvailableResult<SysParam> getHystrixErr(){
        AvailableResult<SysParam> ava = new AvailableResult<>();
        ava.setCode(HttpStatus.BAD_GATEWAY);
        ava.setMsg("hystrix+ribbo进入熔断");
        return ava;
    }


}
